﻿using SheepStorage.Domain.DTO.InputDto;
using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Repository.Interface;
using SheepStorage.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SheepStorage.Service.DataImp
{
    /// <summary>
    /// 菜单模块
    /// </summary>
    public class MenuListService: IMenuListService
    {
        private readonly IBaseRepository<MenuList> menuListRepository;
        private readonly IBaseRepository<MenuPermission> MenuPerbaseRepository;

        //权限表
        private readonly IBaseRepository<Permission> PermissionbaseRepository;
        public MenuListService(IBaseRepository<MenuList> menuListRepository, IBaseRepository<MenuPermission> MenuPerbaseRepository, IBaseRepository<Permission> PermissionbaseRepository)
        {
            this.menuListRepository = menuListRepository;
            this.MenuPerbaseRepository = MenuPerbaseRepository;
            this.PermissionbaseRepository = PermissionbaseRepository;
        }


        //获取菜单列表
        public async Task<List<MenuList>> GetMenuLists(string? MenuName)
        {

            var list = menuListRepository.GetAll().ToList();


            if (!string.IsNullOrEmpty(MenuName))
            {
                list = list.Where(c => c.MenuName.Contains(MenuName)).ToList();
            }

            return list;


        }
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> AddMenus(MenuList input)
        {
            var menures =  menuListRepository.Add(input);
            return menures;

        }



        /// <summary>
        /// 删除菜单-物理删除
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>

       public async Task<int> DeleteMenuList(List<int> menuId)
        {
            if (menuId == null || menuId.Count == 0)
            {
                return -1;
            }
            //批量删除
            var menulist = menuListRepository.GetAll().Where(c => menuId.Contains(c.MenuId)).ToList();
            menuListRepository.DeleteRange(menulist);
            return 1;
        }



        /// <summary>
        /// 菜单反填信息
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public   async Task<MenuList> GetMenuListById(int menuId)
        {

            var list = menuListRepository.GetAll().FirstOrDefault(c=>c.MenuId== menuId);

            return list;
        }
        
        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>

        public async Task<int> UpdateMenuList(MenuList input)
        {
            var res = menuListRepository.Update(input);
            return res;
        }
        













      // /级联方法显示菜单
      public   List<MenuListCascaders> GetMenuListCascaders(int pid=0)
        {
            //实例化
            List<MenuListCascaders> list=new List<MenuListCascaders>();
            //获取所有菜单
            var meunlist = menuListRepository.GetAll().Where(c => c.ParentId == pid).ToList();
            foreach (var item in meunlist)
            {
                //实例化
                MenuListCascaders cascaders = new MenuListCascaders();
                cascaders.Value = item.MenuId;
                cascaders.Label = item.MenuName;
                cascaders.MenuUrl = item.MenuUrl;
    //          var     children =await  GetMenuListCascaders(item.MenuId);
				//if (children.Count > 0)
				//{
				//	cascaders.Children = children;
				//}
               // cascaders.Children = null;
				cascaders.Children =   GetMenuListCascaders(item.MenuId).Count()> 0 ? GetMenuListCascaders(item.MenuId) :null;
				list.Add(cascaders);
            }

            return list;
        }



        /// <summary>
        /// 批量添加菜单权限
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async  Task< int> AddMenuList(MenuPermissionInput input)
        {
            //先删除原有数据
            var oidList = MenuPerbaseRepository.GetAll().Where(c => c.MenuId == input.MenuId).ToList();
            MenuPerbaseRepository.DeleteRange(oidList);


            //批量添加
            foreach (var item in input.PermissionIdList)
            {
                var menuPermission = new MenuPermission()
                {
                    MenuId = input.MenuId,
                    PermissionId = item
                };
                MenuPerbaseRepository.Add(menuPermission);
                
            }


            return 1;   
        }
  
         
        /// <summary>
        /// 列表显示菜单权限
        /// </summary>
        /// <returns></returns>
        public async Task< object> GetMenuPermissions()
        {
    
            //  return meunlist;

            // 获取所有菜单和菜单权限基础数据
            // 一次性获取所有数据
            var menus = menuListRepository.GetAll().ToList();
            var menuPermissions = MenuPerbaseRepository.GetAll().ToList();
            var permissions = PermissionbaseRepository.GetAll().ToList();

            // 使用LINQ构建结果
            var menuPermissionsList = menus
                .Select(menu => new MenuPermissionOutput
                {
                    MenuId = menu.MenuId,
                    MenuName = menu.MenuName,
                    PermissionList = menuPermissions
                        .Where(menuPer => menuPer.MenuId == menu.MenuId)
                        .SelectMany(menuPer => permissions
                            .Where(permiss => permiss.PermissionId == menuPer.PermissionId)
                            .Distinct())
                        .Select(permiss => new Permission
                        {
                            PermissionId = permiss.PermissionId,
                            PermissionName = permiss.PermissionName
                        })
                        .ToList()
                })
                .ToList();

            return menuPermissionsList;




        }
    }
}
